//
//  SWMainViewController.swift
//  SinaWeibo
//
//  Created by AenyMo on 16/3/30.
//  Copyright © 2016年 AenyMo. All rights reserved.
//

import UIKit

class SWMainViewController: UITabBarController {
    
    //MARK: - 中间按钮点击事件
    @objc private func composeButtonDidClick() {
        print(",,,,,,,")
    
    }

    //MARK: - 自定义子控件
    override func viewDidLoad() {
        super.viewDidLoad()

        let mainTabBar = SWTabBar()
        
        //设置自定义tabBar   只读属性, 使用KVC赋值
        setValue(mainTabBar, forKey: "tabBar")
        
        //监听中间按钮点击事件
        mainTabBar.composeButton.addTarget(self, action: #selector(composeButtonDidClick), forControlEvents: .TouchUpInside)
        
        //添加子控制器
        addChildViewControllers();
    }

    //添加所有的子控制器
    private func addChildViewControllers() {
        
        addChildViewController(SWHomeTableViewController(), title: "首页", imageName: "tabbar_home")
        addChildViewController(SWMessageTableViewController(), title: "消息", imageName: "tabbar_message_center")
        addChildViewController(SWDiscoverTableViewController(), title: "发现", imageName: "tabbar_discover")
        addChildViewController(SWProfileTableViewController(), title: "我", imageName: "tabbar_profile")
        
    }
    
    private func addChildViewController(rootVc: UIViewController, title: String, imageName: String) {
        
        //创建导航控制器
        let navController = UINavigationController(rootViewController: rootVc)
        
        //设置文字
        rootVc.title = title;
        //设置图片
        rootVc.tabBarItem.image = UIImage(named: imageName)
        
        //添加到tabBar
        addChildViewController(navController)
    }

}
